package com.easthome.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easthome.auth.mapper.SysRoleMapper;
import com.easthome.auth.service.SysMenuService;
import com.easthome.auth.service.SysRoleMenuService;
import com.easthome.auth.service.SysRoleService;
import com.easthome.common.result.Result;
import com.easthome.common.system.MenuHelper;
import com.easthome.model.system.SysMenu;
import com.easthome.model.system.SysRole;
import com.easthome.model.system.SysRoleMenu;
import com.easthome.vo.system.AssginMenuVo;
import com.easthome.vo.system.SysRoleQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: 朱敦宇
 * @time: 2023/11/6 13:13
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysMenuService sysMenuService;
    @Override
    public Result toAssign(Long roleId) {
        //1.查出所有角色
        List<SysMenu> allMenu = sysMenuService.list();
        //2.根据此用户id找到拥有的菜单(权限)
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId,roleId);
        List<SysRoleMenu> roleMenus = sysRoleMenuService.list(wrapper);
        //3.提取所有角色id
        List<Long> roleIds = roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        for (SysMenu menu : allMenu) {
            if(roleIds.contains(menu.getId())){
                menu.setSelect(true);
            }
        }
        return Result.ok(MenuHelper.toTreeMenu(allMenu));
    }

    @Override
    @Transactional
    public void doAssign(AssginMenuVo assginMenuVo) {
        //1.先删除角色原有菜单
        LambdaUpdateWrapper<SysRoleMenu> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId,assginMenuVo.getRoleId());
        sysRoleMenuService.remove(wrapper);
        //2.向该用户添加新赋予的菜单选项
        List<Long> menuIdList = assginMenuVo.getMenuIdList();
        for (Long aLong : menuIdList) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(aLong);
            sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
            sysRoleMenuService.save(sysRoleMenu);
        }
    }
//    @Override
//    public Page<SysRole> findByPage(Integer page, Integer size, SysRoleQueryVo vo) {
//        Page<SysRole> pages=new Page<>();
//        pages.setSize(2);
//        pages.setCurrent(1);
//        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
////        if(vo == null|| "".equals(vo.getRoleName())){
////            return this.baseMapper.selectPage(pages,null);
////        }
////        else {
////            sysRoleLambdaQueryWrapper.eq(SysRole::getRoleName,vo.getRoleName());
////            return this.baseMapper.selectPage(pages,sysRoleLambdaQueryWrapper);
////        }
//        //2 封装条件，判断条件是否为空，不为空进行封装
//        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
//        String roleName = vo.getRoleName();
//        if(!StringUtils.isEmpty(roleName)) {
//            //封装 like模糊查询
//            wrapper.like(SysRole::getRoleName,roleName);
//        }
//        return Result.ok()
//    }
}
